Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Security.Cryptography

Namespace DPRAjaxControlToolkit


    Public Class StringHelper
        Public Shared Function HexStringToBytes(ByVal hex As String) As Byte()
            If hex.Length = 0 Then
                Return New Byte() {0}
            End If

            If (hex.Length Mod 2) = 1 Then
                hex = "0" & hex
            End If

            Dim retorno((hex.Length / 2) - 1) As Byte

            For i As Integer = 0 To (hex.Length / 2) - 1
                retorno(i) = Byte.Parse(hex.Substring(2 * i, 2), System.Globalization.NumberStyles.AllowHexSpecifier)
            Next

            Return retorno

        End Function

        Public Shared Function BytesToHexString(ByVal matriz As Byte()) As String
            Dim hexString As New StringBuilder(64)

            For Each i As Byte In matriz
                hexString.Append(String.Format("{0:X2}", i))
            Next

            Return hexString.ToString()

        End Function

        Public Shared Function BytesToDecString(ByVal matriz As Byte()) As String
            Dim decString As New StringBuilder(64)

            For Each i As Byte In matriz
                decString.Append(String.Format(IIf(Array.IndexOf(matriz, i) = 0, "{0:D3}", "-{0:D3}"), i))
            Next

            Return decString.ToString()
        End Function

        Public Shared Function ASCIIBytesToString(ByVal matriz As Byte()) As String
            Dim enc As New System.Text.ASCIIEncoding
            Return enc.GetString(matriz)
        End Function

        Public Shared Function UTF16BytesToString(ByVal matriz As Byte()) As String
            Dim enc As New System.Text.UnicodeEncoding
            Return enc.GetString(matriz)
        End Function

        Public Shared Function UTF8BytesToString(ByVal matriz As Byte()) As String
            Dim enc As New System.Text.UTF8Encoding
            Return enc.GetString(matriz)
        End Function

        Public Shared Function UTF7BytesToString(ByVal matriz As Byte()) As String
            Dim enc As New System.Text.UTF7Encoding
            Return enc.GetString(matriz)
        End Function

        Public Shared Function ToBase64(ByVal matriz As Byte()) As String
            Return Convert.ToBase64String(matriz)
        End Function

        Public Shared Function FromBase64(ByVal base64 As String) As Byte()
            Return Convert.FromBase64String(base64)
        End Function

    End Class

End Namespace